package com.cirkana.viewer.map;

import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebView;

import com.cirkana.CirkanaActivity;
import com.cirkana.CirkanaUtil;
import com.cirkana.serial.SerializationException;
import com.cirkana.viewer.R;
import com.cirkana.viewer.comm.Communicator;
import com.cirkana.viewer.location.ChangeProviderDialog;
import com.cirkana.viewer.location.ChangeProviderPassData;
import com.cirkana.viewer.location.MapLocation;
import com.cirkana.viewer.location.MockLocation;
import com.cirkana.viewer.location.select.SelectLocationActivity;
import com.cirkana.viewer.location.select.SelectLocationPassData;
import com.cirkana.viewer.qr.QRReaderActivity;
import com.cirkana.viewer.share.ShareActivity;
import com.cirkana.viewer.share.SharePassData;

public class MapActivity extends CirkanaActivity {

	public static final int REQUEST_CHANGELOCATION = 1;
	public static final int REQUEST_CHANGEPROVIDER = 2;
	public static final int REQUEST_SHARE = 3;

	public static final int RESULT_MAP_OK = 1;

	private WebView mapWebView;

	private MapPassData data;
	private Communicator communicator;

	private Location currentLocation;
	private String currentLocationProvider = "gps";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map);

		this.mapWebView = (WebView) findViewById(R.id.wvMap);

		this.data = (MapPassData) this.getRequest(MapPassData.class);

		this.communicator = new Communicator();
		this.communicator.addTrackerMessages(this.data.getTracker());
		MapLocation.start(currentLocationProvider, this.communicator);
		this.currentLocation = MapLocation
				.getLastKnownLocation(currentLocationProvider);
		if (this.currentLocation == null)
			this.currentLocation = CirkanaUtil.defaultLocation;
		this.communicator.addChangeLocationMessage(this.currentLocation);

		this.mapWebView.getSettings().setJavaScriptEnabled(true);
		this.mapWebView.loadUrl("file:///android_asset/mapviewer.html");
		this.mapWebView.addJavascriptInterface(communicator, "comm");
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.map_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_map_change_location:
			try {
				startActivityForResult(SelectLocationActivity.class,
						REQUEST_CHANGELOCATION, new SelectLocationPassData(
								this.currentLocationProvider,
								this.currentLocation.getLatitude(),
								this.currentLocation.getLongitude()));
			} catch (SerializationException e) {
				openMessageBox("Error", e.getMessage());
			}
			return true;

		case R.id.menu_map_change_location_provider:
			try {
				startActivityForResult(ChangeProviderDialog.class,
						REQUEST_CHANGEPROVIDER, new ChangeProviderPassData(
								this.currentLocationProvider));
			} catch (SerializationException e) {
				openMessageBox("Error", e.getMessage());
			}
			return true;

		case R.id.menu_map_share_twitter:
			try {
				startActivityForResult(ShareActivity.class, REQUEST_SHARE,
						new SharePassData(ShareActivity.SHARE_CHANNEL_TWITTER));
			} catch (SerializationException e) {
				openMessageBox("Error", e.getMessage());
			}
			return true;

		case R.id.menu_map_catch_picture:
			return true;

		case R.id.menu_map_catch_qr:
			startActivity(new Intent(this, QRReaderActivity.class));
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		switch (requestCode) {
		case REQUEST_CHANGELOCATION:
			switch (resultCode) {
			case SelectLocationActivity.RESULT_CHANGELOCATION_OK:
				try {
					SelectLocationPassData result = (SelectLocationPassData) getResponse(
							data, SelectLocationPassData.class);
					if (result.hasLocation()) {
						this.currentLocation = new MockLocation(result
								.getLatitude(), result.getLongitude());
						this.communicator
								.addChangeLocationMessage(this.currentLocation);
					}
				} catch (SerializationException e) {
					showErrorMessage(e);
				}
				break;
			case CirkanaActivity.RESULT_CANCEL:
				break;
			}
			break;

		case REQUEST_CHANGEPROVIDER:
			switch (resultCode) {
			case ChangeProviderDialog.RESULT_CHANGEPROVIDER_OK:
				try {
					ChangeProviderPassData result = (ChangeProviderPassData) getResponse(
							data, ChangeProviderPassData.class);
					this.currentLocationProvider = result.getLocationProvider();
					MapLocation.start(this.currentLocationProvider,
							this.communicator);
				} catch (SerializationException e) {
					showErrorMessage(e);
				}
				break;

			case CirkanaActivity.RESULT_CANCEL:
				break;
			}
			break;

		case REQUEST_SHARE:
			switch (resultCode) {
			case ShareActivity.RESULT_SHARE_OK:
				break;
			case CirkanaActivity.RESULT_CANCEL:
				break;
			}
		}

	}
}